package com.icinfo.cloud.provider.common.zlb.utils;

import cn.hutool.core.codec.Base64;
import com.icinfo.framework.core.exception.BusinessException;
import com.taobao.api.internal.util.DESUtil;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;

/**
 * 简单的des加密工具
 *
 **/
public class SimpleDesUtils extends DESUtil {

    /**
     * 秘钥
     */
    private final static String PWD = "FF8080815D551320015D58A47FEB001F";

    /**
     * 加密返回byte
     *
     * @param datasource
     * @return
     **/
    private static byte[] encrypt(byte[] datasource) {
        try {
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(PWD.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            return cipher.doFinal(datasource);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密返回byte
     *
     * @param src
     * @return
     **/
    private static byte[] decrypt(byte[] src) {
        byte[] resultByte = null;
        try {
            DESKeySpec desKey = new DESKeySpec(PWD.getBytes());

            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, securekey, new SecureRandom());
            resultByte = cipher.doFinal(src);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultByte;
    }

    /**
     * 加密返回base64字符串
     *
     * @param sourceStr
     * @return
     **/
    public static String desEncrypt(String sourceStr) {
        try {
            byte[] enStr = encrypt(sourceStr.getBytes("UTF-8"));
            String strMi = new String(Base64.encode(enStr));
            return strMi.replaceAll("\r|\n", "");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new BusinessException("加密失败");
        }
    }

    /**
     * 解密
     *
     * @param encStr
     * @return
     **/
    public static String desDecrypt(String encStr) {
        try {
            // 转成BASE64
            byte[] byteFina = decrypt(Base64.decode(encStr.getBytes("UTF-8")));
            String strMing = new String(byteFina, "UTF-8");
            return strMing.replaceAll("\r|\n", "");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("解密失败");
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String enResult = desEncrypt("{\"start\":0,\"length\":10,\"params\":{\"userUID\":\"fb4f87c27f2111e6b98600188b839ae1\",\"deptCode\":\"33010000\"}}");
        System.out.println("加密：" + enResult);
        System.out.println("解密:" + desDecrypt(enResult));
    }

}
